package com.etennis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etennis.mapper.ClubMapper;
import com.etennis.mapper.EventsMapper;
import com.etennis.mapper.MemberMapper;
import com.etennis.mapper.PlayersMapper;
import com.etennis.pojos.entity.Club;
import com.etennis.pojos.entity.Events;
import com.etennis.pojos.entity.Member;
import com.etennis.pojos.entity.Players;
import com.etennis.pojos.vo.app.MatchVO;
import com.etennis.pojos.vo.app.UserVO;
import com.etennis.service.IEventsService;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EventsServiceImpl extends ServiceImpl<EventsMapper, Events> implements IEventsService {
    @Autowired
    EventsMapper eventsMapper;
    @Autowired
    ClubMapper clubMapper;
    @Autowired
    PlayersMapper playersMapper;
    @Autowired
    MemberMapper memberMapper;

    /**
     * 可报名赛事
     * @return
     */
    @Override
    public List<MatchVO> matchList() {
        Date now=new Date();
        LambdaQueryWrapper<Events> wrapper=new LambdaQueryWrapper<Events>();
        wrapper.ge(Events::getEnrollEndDate,now).le(Events::getEnrollStartDate,now);
        List<Events> events = eventsMapper.selectList(wrapper);
        LambdaQueryWrapper<Players> wrapper1= Wrappers.lambdaQuery(Players.class)
                .eq(Players::getMemberId,SecurityUtils.getLoginUserId())
                .eq(Players::getIsExit,"0");
        List<Players> players = playersMapper.selectList(wrapper1);
        Set<Long> registerIds = players.stream()
                .map(Players::getEventId)
                .collect(Collectors.toSet());
        List<MatchVO> res = events.stream().
                filter(each->!registerIds.contains(each.getId())).
                map(each -> {
            MatchVO matchVO = BeanCopyUtils.copyBean(each, MatchVO.class);
            Club club = clubMapper.selectById(each.getClubId());
            matchVO.setClubName(club.getName());
            return matchVO;
        }).collect(Collectors.toList());
        return res;
    }

    @Override
    public void signup(Long id) {
        Players players=new Players();
        players.setMemberId(SecurityUtils.getLoginUserId());
        players.setEventId(id);
        players.setEnrollTime(new Date());
        players.setIsExit("0");
        playersMapper.insert(players);
    }

    /**
     * 根据用户id返回已报名赛事
     * @return
     */
    @Override
    public List<MatchVO> registeredMatch() {
        Long userId = SecurityUtils.getLoginUserId();
        LambdaQueryWrapper<Players> wrapper= Wrappers.lambdaQuery(Players.class)
                .eq(Players::getMemberId,userId)
                .eq(Players::getIsExit,"0");
        List<Players> players = playersMapper.selectList(wrapper);
        List<MatchVO> list = players.stream().map(each -> {
            Events events = eventsMapper.selectById(each.getEventId());
            MatchVO matchVO = BeanCopyUtils.copyBean(events, MatchVO.class);
            Club club = clubMapper.selectById(events.getClubId());
            matchVO.setClubName(club.getName());
            return matchVO;
        }).collect(Collectors.toList());

        return list;
    }

    @Override
    public List<UserVO> registeredPlayers(Long id) {
        LambdaQueryWrapper<Players> wrapper=Wrappers.lambdaQuery(Players.class)
                .eq(Players::getEventId,id);
        List<Players> players = playersMapper.selectList(wrapper);
        List<UserVO> list = players.stream().map(each -> {
            Member member = memberMapper.selectById(each.getMemberId());
            UserVO userVO = new UserVO();
            userVO.setId(member.getId());
            userVO.setName(member.getName());
            return userVO;
        }).collect(Collectors.toList());
        return list;
    }
}
